﻿using System;
using System.Collections.Generic;
using System.Text;
using WatiN.Core.Interfaces;
using com.gargoylesoftware.htmlunit;
using com.gargoylesoftware.htmlunit.html;

namespace WatiN.Core.HtmlUnit
{
	public class HUDocument : INativeDocument
	{
		HUBrowser browser;
		HtmlPage page;

		public HUDocument(HtmlPage page, HUBrowser browser)
		{
			this.page = page;
			this.browser = browser;			
		}

		private HtmlElement GetBody()
		{
			return page.getBody();
		}

		#region INativeDocument Members
		public object Object
		{
			get 
			{ 
			
				return new HUElement(GetBody(), browser); 
			
			}
		}

		public object Objects
		{
			get 
			{
				List<INativeElement> result = new List<INativeElement>();

				java.util.Iterator iter = page.getAllHtmlChildElements().iterator();

				while (iter.hasNext())
				{
					var elem = iter.next() as HtmlElement;

					if (elem != null)
						result.Add(new HUElement(elem, browser));
				}

				return result;
			}
		}

		public INativeElement Body
		{
			get { return new HUElement(GetBody(), browser); }
		}

		public string Url
		{
			get 
			{
				string url = string.Empty;

				if (page != null)
					url = page.getWebResponse().getUrl().toURI().toString();
				

				return url;
			}
		}

		public string Title
		{
			get { return page.getTitleText(); }
		}

		public INativeElement ActiveElement
		{
			get { return new HUElement(page.getFocusedElement(), browser); }
		}

		public void RunScript(string scriptCode, string language)
		{
			page.executeJavaScript(scriptCode);
		}

		public string JavaScriptVariableName 
		{
			get { return string.Empty; }
		}

		public string GetPropertyValue(string propertyName)
		{
			return string.Empty;
		}

		#endregion
	}
}
